% should run Proj_to_individualBrain.sh before this
function[] = func_WriteParcellation_to_annotation_fs4(ParPath, sub, nIter)
ParPath
sub

%recon_dir = '/autofs/cluster/nexus/5/users/Hesheng/IterativeParcellation/Com_Recon/'

load 17NetworkLUT

offset = 0% 14;
hemi = {'lh'; 'rh'};

    for h =1:length(hemi)
        clear parc
        hm = hemi{h};


        dataPath = [ParPath '/' sub '/Iter_' num2str(nIter)]
        outPath = dataPath;
       
        [v, L,cto]= read_annotation(['/autofs/space/bidlin4_001/users/Share/Templates/Surf/fsaverage4/label/',hm,'.aparc.annot']);
        ct = newct;
        ct.table(1+offset,4)=255;

        L(1:end) = ct.table(1,5);
        HL =L;

        % Merge all the parcellation networks to one map
        for net =1:19
            parc(net,:) = load_mgh([dataPath '/' 'Network_' num2str(net) '_' hm '.mgh']);
            if net ==1
                x = parc(net,:);
                x(find(x<1))=0;
                x =5*x;
                parc(net,:) =x; % this is the ventrical or useless area, make it very big.
            end

        end
        %

        [conf, netmember] = max(parc);

     
        for k =1:19

            idx= find(netmember==k);
            L(idx) = ct.table(k+offset,5);

        end


        fname =[outPath,  '/',hm,'_parc_result_fs4.annot']
        write_annotation(fname, v, L, ct);

    end



